package com.top.mall.jetpack.ma

import com.top.mall.app.BaseApplication
import com.top.mall.base.BaseManager
import com.top.mall.entity.*
import rxhttp.RxHttp
import rxhttp.await

class CartManager : BaseManager() {

    /**
     * 购物车列表
     *
     * @param shopId 门店id
     * */
    suspend fun cartIndex(): CartListResponse =
        RxHttp.get("/api/cart/index")
            .add("shop_id", BaseApplication.SHOP_ID)
            .await()

    /**
     * 购物车加入
     *
     * @param shopId 门店id
     * @param goodsId 物品id
     * @param goodsNum 加购数量
     * */
    suspend fun cartAdd(goodsId: Int, goodsNum: Int): CartAddResponse =
        RxHttp.postForm("/api/cart/add")
            .add("shop_id", BaseApplication.SHOP_ID)
            .add("goods_id", goodsId)
            .add("goods_num", goodsNum)
            .await()

    /**
     * 购物车数量加减
     *
     * @param cartId 购物车id
     * @param goodsNum 加购数量
     * */
    suspend fun cartChangeNum(cartId: Int, goodsNum: Int): CartChangeNumResponse =
        RxHttp.postForm("/api/cart/changeNum")
            .add("cart_id", cartId)
            .add("goods_num", goodsNum)
            .await()

    /**
     * 购物车删除
     *
     * @param cartIds 购物车id数组
     * */
    suspend fun cartDelete(cartIds: Array<Int>): CartDeleteResponse =
        RxHttp.postForm("/api/cart/delete")
            .add("cart_ids[]", cartIds)
            .await()

    suspend fun cartSettlement(cartList: Array<Int>): SettlementCartResponse {
        val http = RxHttp.postForm("/api/cart/settlement")
            .add("shop_id", BaseApplication.SHOP_ID)
        for (item in cartList) {
            http.add("cart_list[]", item)
        }
        return http.await()
    }

}